package com.lzw.DailyAudit.service.dateAggregation_02.DailyAudit.service.dealSourceData_01;


import cn.hutool.core.util.StrUtil;
import com.hooya.fa.eu.biz.DailyAudit.entity.CkDict;
import com.hooya.fa.eu.biz.DailyAudit.entity.dto.StoreCorpDTO;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyCkDictMapper;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyMetaStoreMapper;
import com.hooya.fa.eu.biz.DailyAudit.mapper.DailyXsjl2303Mapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class Pre_Test {

    @Autowired
    private DailyXsjl2303Mapper xsjl2303Mapper;

    @Autowired
    private DailyMetaStoreMapper metaStoreMapper;

    @Autowired
    private DailyCkDictMapper ckDictMapper;

	public void preTest(String currentDate) {

        // 加载fa_eu.meta_store,得到所有的 店铺-公司 映射数据 （store-corp）
        List<StoreCorpDTO> storeCorpList = metaStoreMapper.getStoreAndCorp();
        Map<String, String> storeCorpDic = storeCorpList.stream().collect(Collectors.toMap(StoreCorpDTO::getStore, StoreCorpDTO::getCorp));
        log.info("成功加载了 {} 条店铺-公司映射数据", storeCorpDic.size());


        // 测试是否有新店铺
        List<String> stores = xsjl2303Mapper.getStores();
        // 将现有店铺转为小写存入集合
        List<String> existStoreLower = storeCorpDic.keySet().stream()
                .map(String::toLowerCase)
                .collect(Collectors.toList());
        log.info("已存在的店铺数量： {}", existStoreLower.size());

        //用于存储新店铺----不存在于existStoreLower中的store
        List<String> newStores = stores.stream()
                .filter(f -> StrUtil.isNotBlank(f) && !existStoreLower.contains(f.toLowerCase()))
                .collect(Collectors.toList());
        log.info("新的店铺数量： {}", newStores.size());

        if (!newStores.isEmpty()) {
            log.warn("新增店铺列表: {}", newStores);
            throw new RuntimeException("新增店铺列表: " + newStores);
        } else {
            log.info("当天没有新增的店铺，测试新店铺通过");
        }


        // 查询OEU订单
        List<String> oeuOrderIds = xsjl2303Mapper.getOeuIds();
        // 从数据库获取oeuOrderMap字典
        List<CkDict> oeuOrderDictList = ckDictMapper.selectByName("oeuOrderMap");

        //构建映射关系
        List<String> oeuOrderDictParamKeys = oeuOrderDictList.stream()
                .map(CkDict::getParamKey)
                .collect(Collectors.toList());

        // 检查订单是否存在
        List<String> newOeuOrders = oeuOrderIds.stream()
                .filter(f -> StrUtil.isNotBlank(f) && !oeuOrderDictParamKeys.contains(f))
                .collect(Collectors.toList());

        if (!newOeuOrders.isEmpty()) {
            log.info("新增店铺订单信息集合: {}", newOeuOrders);
            throw new RuntimeException("新增店铺订单信息集合: " + newOeuOrders);
        } else {
            log.info("当天没有oeu订单，测试OEU订单通过");
        }

		log.info("preTest执行成功");
    }


}